package com.yuyou.fn.settlement.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.settlement.entity.NewPayRecord;
import com.yuyou.fn.settlement.entity.PayRecord;
import com.yuyou.fn.settlement.mapper.PayRecordMapper;
import com.yuyou.fn.settlement.service.INewPayRecordService;
import com.yuyou.fn.settlement.service.IPayRecordService;
import com.yuyou.fn.settlement.vo.PayRecordQueryVo;
import com.yuyou.fn.settlement.vo.PayWay;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 *
 * PayRecord 表数据服务层接口实现类
 *
 */
@Service
public class PayRecordServiceImpl extends SuperServiceImpl<PayRecordMapper, PayRecord,Long> implements IPayRecordService {

    @Resource
    private INewPayRecordService newPayRecordService;

    @Override
    public Page<PayRecord> findPayRecordByQuery(PayRecordQueryVo payRecordQueryVo, int pageNo, int pageSize) {

        if(payRecordQueryVo.getEndDate() != null ){
            payRecordQueryVo.setEndDate(DateUtils.addDays(payRecordQueryVo.getEndDate(),1));
        }

        Example<PayRecord> example = PayRecord.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,payRecordQueryVo);
        criteria.andEqualTo("hidden" , 0 );
        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public void migrate() {
        int pageNo = 1 ;
        while (true){
            Example<PayRecord> example = PayRecord.createExample();
            example.createCriteria().andEqualTo("hidden" , 0 );
            example.orderBy("payRecordId");

            Page<PayRecord> page = findPageByExample(example, pageNo++, 1000);

            if(page.getRecords() == null || page.getRecords().isEmpty() ){
                break;
            }

            List<NewPayRecord> newPayRecords = Lists.transform(page.getRecords(),payRecord -> {
                NewPayRecord newPayRecord  = new NewPayRecord();
                newPayRecord.setPayRecordId(payRecord.getPayRecordId());
                newPayRecord.setBusOrderId(payRecord.getBusOrderId());
                newPayRecord.setBusType(payRecord.getBusType());
                newPayRecord.setAmount(payRecord.getAmount());
                newPayRecord.setFirstPay(1);
                newPayRecord.setCampusId(payRecord.getCampusId());
                newPayRecord.setOperator(payRecord.getOperator());
                newPayRecord.setCreateTime(payRecord.getCreateTime());
                newPayRecord.setUpdateTime(payRecord.getUpdateTime());

                PayWay payWay = new PayWay();
                payWay.setAmount(payRecord.getAmount());
                payWay.setPayWay(payRecord.getPayWay());
                payWay.setMorePayInfo(payRecord.getPayInfoExt());
                newPayRecord.setPayWays(JSON.toJSONString(Arrays.asList(payWay)));
                return newPayRecord ;
            });
            try{
                newPayRecordService.insertBatch(newPayRecords);
            }catch (Exception e){
                for(NewPayRecord newPayRecord : newPayRecords ){
                    newPayRecordService.updateSelectiveById(newPayRecord);
                }
            }



        }




    }
}